﻿/**
 * EasyInsert 4.0
 * http://IlikejQuery.com/EasyInsert
 * Download by http://www.codefans.net
 * @Creator   wo_is神仙 <jsw0528@MrZhang.net>
 * @Depend    jQuery 1.4+
**/
(function($){
    $.fn.extend({
        easyinsert:function(F){
            F=$.extend({
                clicker:null,
                wrap:"li",
                name:"ly[]",
                type:"text",
                value:"",
                maxlength:20,
                className:"ly",
                toplimit:0,
                initValue:null
                },
                F||{});
                var E={
                    remove:'<a href="#nogo" class="remove">\u79fb\u9664</a>',
                    error1:"\u53c2\u6570\u914d\u7f6e\u9519\u8bef\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c\u8bf7\u68c0\u67e5\u3002",
                    error2:"\u53c2\u6570\u914d\u7f6e\u9519\u8bef\uff0c\u6bcf\u7ec4\u521d\u59cb\u5316\u503c\u90fd\u5fc5\u987b\u662f\u6570\u7ec4\uff0c\u8bf7\u68c0\u67e5\u3002"},
                    B=$(this),
                    _=true,
                    D=new Array(F.name,F.type,F.value,F.maxlength,F.className),
                    C=new Array();
                    $.each(D,function(D,A){
        if($.isArray(A))
        C[D]=A;
        else{
        C[D]=new Array();
        if(D===0)C[0].push(A);
        else 
        $.each(C[0],function(){
        C[D].push(A)
        })}
        if(
        C[D].length!==C[0].length){
            _=false;B.text(E.error1)}});
            if(_){
                var A=!F.clicker?B.next():$(F.clicker);
                A.bind("click",function(){
                    var D=B.children(F.wrap).length,_=F.toplimit===0?false:(D<F.toplimit?false:true);
                    if(!_){var A=$("<"+F.wrap+">").appendTo(B);$.each(C[0],function(B){
                        switch(C[1][B]){case"select":var _="";
                        $.each(C[2][B],function(A,$){
                            _+="<option value='"+$+"'>"+A+"</option>"
                            });$("<select>",{name:C[0][B],className:C[4][B]}).append(_).appendTo(A);
                            break;
                            case"custom":A.append(C[2][B]);break;
                            default:
                            $("<input />",{
                                name:C[0][B],type:C[1][B],value:C[2][B],maxlength:C[3][B],className:C[4][B]
                                }).appendTo(A)}});
                                A=B.children(F.wrap);
                                D=A.length;if(D>1){A.last().append(E.remove);
                                if(D===2)A.first().append(E.remove)}A.find(".remove").click(function(){
                                    $(this).parent().remove();
                                    D=B.children(F.wrap).length;if(D===1)B.find(".remove").remove();
                                    return false
                                    })}return false});
                                    if(
                                    $.isArray(F.initValue))$.each(F.initValue,function(F,_){
                                        if(!$.isArray(_)){B.empty().text(E.error2);
                                    return false
                                    }else if(_.length!==C[0].length){B.empty().text(E.error1);
                                    return false}
                                    var D=new Array();
                                    $.each(_,function(_,$){D[_]=C[2][_];C[2][_]=$});
                                    A.click();$.each(D,function(_,$){C[2][_]=$})});
                                    else A.click()}}})
                                    })(jQuery)